package com.nbicc.catalog.po;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.nbicc.catalog.myenum.CatTypeEnum;
import com.nbicc.common.TreeNode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;

@ApiModel(value="目录对象" ,
        description = "链接目录：目录下，有且只有一个新闻时，可以转换成链接目录。" +
                "用户自点击该目录时，不是跳转出新闻列表，而是直接展示其中那唯一的新闻。" +
                "后端只做标记，前端做逻辑控制。")
@Data
public class Cat implements TreeNode {

    //对应的数据库字段名
    public final static String FIELD_ID = "id";
    public final static String FIELD_TYPE = "type";
    public final static String FIELD_ALLOWCHILD = "allowChild";

    @ApiModelProperty(value="目录id")
    private int id;
    @ApiModelProperty(value="目录名")
    private String name;
    @ApiModelProperty(value="目录父id" , notes = "如果为根目录就传0")
    private int pid;
    @ApiModelProperty(value="目录类型" , notes = "现为两种(普通目录和链接目录)")
    private CatTypeEnum type;
    @ApiModelProperty(value="是否可以有子节点" , notes = "如果是链接目录，不允许有子节点")
    @JsonIgnore
    private boolean allowChild = true;

    @ApiModelProperty(value="树状展示时,其子目录")
    private List<TreeNode> children = new ArrayList<TreeNode>();

    @Override
    public String id() {
        return id+"";
    }

    @Override
    public String pid() {
        return pid+"";
    }

    @Override
    public List<TreeNode> children() {
        return children;
    }

    @Override
    public void setChildren(List<TreeNode> children) {
        this.children.addAll(children);
    }

    @ApiIgnore
    @JsonIgnore
    public boolean isRoot(){
        if(id == 0) return true;
        return false;
    }

}
